package com.tenglong.teacher.ui.setting;

import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.cuotibao.zhuoxiao.basics.GlobalConstant;
import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.CurrentVersionInfo;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.common.activity.BaseActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class VersionActivity extends BaseActivity implements View.OnLongClickListener {

    private TextView mVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_version);
        initView();
        setListeners();
    }

    private void setListeners() {
        findViewById(R.id.checkVersion).setOnClickListener(this);
        mTitleView.setOnLongClickListener(this);
    }

    private void initView() {
        setTitle(getString(R.string.abortMe));
        mVersion = findViewById(R.id.version);
        try {
            mVersion.setText("Version " + getPackageManager().getPackageInfo(getPackageName(), 0).versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.checkVersion:
                checkVersion();
                break;
            case R.id.action_text:
                break;
        }
    }

    private void checkVersion() {
        showProgressDialog("版本检测中...");
        CreateRequestEntity.getWebServiceLogin().getVersion().enqueue(new Callback<BaseResult<CurrentVersionInfo>>() {
            @Override
            public void onResponse(Call<BaseResult<CurrentVersionInfo>> call, Response<BaseResult<CurrentVersionInfo>> response) {
                dismissProgressDialog();
                if (ResultVerifier.isSucceed(response)) {
                    if (response.body().data != null) {
                        try {
                            if (getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_CONFIGURATIONS).versionCode < response.body().data.versionCode) {
                                ToastUtils.showToast("请去登录页面更新下载");
                            } else {
                                ToastUtils.showToast("已是最新版本");
                            }
                        } catch (PackageManager.NameNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    ToastUtils.showCenterToast(ResultVerifier.serceErrers(response));
                }
            }

            @Override
            public void onFailure(Call<BaseResult<CurrentVersionInfo>> call, Throwable t) {
                dismissProgressDialog();
                ResultVerifier.systerErrers(t);
            }
        });
    }

    @Override
    public boolean onLongClick(View v) {
        SharedPreferences spDebugMode = getSharedPreferences("debug_mode", 0);
        SharedPreferences.Editor ed = spDebugMode.edit();
        ed.putBoolean(GlobalConstant.KEY_OPEN_DEBUG_RELEASE, true);
        ed.commit();
        return true;
    }
}
